package org.yibinxueyuan.drawcard.controller;

import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.yibinxueyuan.drawcard.entity.Cardinfo;
import org.yibinxueyuan.drawcard.service.CardInfoService;

import javax.annotation.Resource;

/**
 *  这个类也已经弃用,已经用ajax实现了
 * 对card表的数据进行增删改查的操作 的 控制
 */
@Controller
public class Operation extends ApplicationObjectSupport {
    @Resource(name = "CardinfoServiceImpl")
    CardInfoService cardInfoService;

    /**
     * 跳转到增加卡片页面
     * @param model
     * @return
     */
    @RequestMapping(value = "/card/toinsert")
    public String toinsertCard(Model model){
        Cardinfo cardinfo = (Cardinfo) getApplicationContext().getBean("cardinfoBean2");
        model.addAttribute("message","增加卡片");
        model.addAttribute("cardinfo",cardinfo);
        return  "operation/insertCard";
    }

    /**
     * 增加卡片的业务处理
     * @param cardinfo
     * @param model
     * @return
     */
    @RequestMapping(value = "/card/insert")
    public String insertCard(Cardinfo cardinfo,Model model){
        Cardinfo cardinfo2 = (Cardinfo) getApplicationContext().getBean("cardinfoBean2");
        int number = cardInfoService.insertNewCard(cardinfo);
        if(number >= 1){
            model.addAttribute("message","增加成功,请继续其他操作");
            //传递空值给前端
            model.addAttribute("cardinfo",cardinfo2);
            return "operation/insertCard";
        }else {
            //传递当前错误数据给前端,方便排错
            cardinfo.setCardChancef(null);
            cardinfo.setCardChancel(null);
            model.addAttribute("cardinfo",cardinfo);
            model.addAttribute("message","增加失败,请检查数据后重试");
            return "operation/insertCard";
        }
    }

    /**
     *  跳转到修改页面
     */
    @RequestMapping(value = "card/toupdate")
    public String toUpdateCard(int cardId,Model model){
        Cardinfo cardinfo = cardInfoService.queryCardById(cardId);
        model.addAttribute("cardinfo",cardinfo);
        model.addAttribute("message","修改卡片");
        return "operation/updateCard";
    }

    /**
     * 更新卡片的业务逻辑
     */
    @RequestMapping(value = "card/update")
    public String updateCard(Cardinfo cardinfo,Model model,RedirectAttributes attr){
        int number = cardInfoService.updateCardinfoById(cardinfo);
        if(number>=1){
            String message = "修改成功";
            attr.addAttribute("message",message);
            return "redirect:/cardmanage";
        }else{
            model.addAttribute("message","更新失败,请检查数据后重试");
            return "operation/updateCard";
        }
    }


    /**
     * 删除卡片的业务逻辑
     */
    @RequestMapping(value = "/card/delete")
    public String deleteCard(int cardId, RedirectAttributes attr){
        int number = cardInfoService.deleteCardinfoById(cardId);
        String message = null;
        if(number>=1){
            message = "删除成功";
            attr.addAttribute("message",message);
        }else{
            message = "删除失败";
            attr.addAttribute("message",message);
        }
        return "redirect:/cardmanage";
    }



}
